package com.lz.controller;

import com.alibaba.fastjson.JSONObject;
import com.lz.beans.AppUser;
import com.lz.service.LoginService;
import com.lz.service.RegisterService;
import com.lz.utils.AppMD5Util;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by lz on 2018/5/10.
 */
@Controller
@RequestMapping("/register")
public class RegisterController {

    @Autowired
    private RegisterService registerService;

    @Autowired
    private LoginService loginService;

    /**
     * 去注册页面
     * @return
     */
    @RequestMapping("")
    public String toRegister(){
        return "/register";
    }

    /**
     * 注册方法
     * @param appUser
     * @return
     */
    @RequestMapping("/up")
    @ResponseBody
    public JSONObject signUp(@ModelAttribute AppUser appUser){
        String username = appUser.getUsername();
        String password = appUser.getPassword();
        JSONObject jsonObject = new JSONObject();
        if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(password)){
            List<Map<String, Object>> user = loginService.findUser(username);
            if (user.size() > 0){
                jsonObject.put("msg","用户已经存在");
            } else {
                password = AppMD5Util.MD5(password);//md5加密
                appUser.setPassword(password);
                int i = 0;
                try {
                    i = registerService.register(appUser);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (i > 0){
                    jsonObject.put("msg","注册成功");
                    jsonObject.put("username",username);
                } else {
                    jsonObject.put("msg","系统错误");
                }
            }
        } else {
            jsonObject.put("msg","正确填写注册信息");
        }
        return jsonObject;
    }

    /**
     * 去修改密码页面
     * @return
     */
    @RequestMapping("/toModify")
    public String toModify(){
        return "/modify";
    }

    /**
     * 修改密码
     * @param request
     * @return
     */
    @RequestMapping("/modify")
    @ResponseBody
    public JSONObject modifyPassword(HttpServletRequest request){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        JSONObject jsonObject = new JSONObject();
        if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(password)){
            List<Map<String, Object>> user = loginService.findUser(username);
            if (user.size() > 0){
                password = AppMD5Util.MD5(password);
                int id = 0;
                try {
                    id = registerService.modifyPassword(password, user.get(0).get("id"));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (id > 0){
                    jsonObject.put("msg","修改成功");
                } else {
                    jsonObject.put("msg","系统错误");
                }
            } else {
                jsonObject.put("msg","用户不存在，请注册");
            }
        } else {
            jsonObject.put("msg","正确填写注册信息");
        }
        return jsonObject;

    }

    /**
     * 修改密码
     * @param appUser
     * @return
     */
    @RequestMapping("/modifyInfo")
    @ResponseBody
    public JSONObject modifyInfo(@ModelAttribute AppUser appUser){
        String username = appUser.getUsername();
        JSONObject jsonObject = new JSONObject();
        if (StringUtils.isNotBlank(username)){
            List<Map<String, Object>> user = loginService.findUser(username);
            if (user.size() > 0){
                int id = 0;
                try {
                    id = registerService.modifyInfo(appUser, user.get(0).get("id"));
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (id > 0){
                    jsonObject.put("msg","修改成功");
                } else {
                    jsonObject.put("msg","系统错误");
                }
            } else {
                jsonObject.put("msg","用户不存在，请注册");
            }
        } else {
            jsonObject.put("msg","正确填写注册信息");
        }
        return jsonObject;

    }

}
